package com.example.AIstudy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.AIstudy.domain.StuAndProSet;
import com.example.AIstudy.domain.User;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.StuAndProSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/stuandProset")
@Api(tags = "用户与套题关联信息的管理API")
public class StuAndProSetController {

    @Autowired
    StuAndProSetService stuAndProSetService;

    //新增一个用户做套题的记录
    @PostMapping("")
    @ApiOperation("新增用户做题的记录")
//    @PreAuthorize("hasAnyAuthority(#stuAndProSet.stuID.toString())")
    StuAndProSet insertStuAndSet(@ApiParam("做题记录")@RequestBody StuAndProSet stuAndProSet)throws AIstudyException{
        stuAndProSetService.insertStuAndSet(stuAndProSet);
        return stuAndProSet;
    }

    //更新做题的记录
    @PutMapping("")
    @ApiOperation("更新用户做题的记录")
//    @PreAuthorize("hasAnyAuthority(#stuAndProSet.stuID.toString())")
    public void updateStuAndSet(@ApiParam("做题记录")@RequestBody StuAndProSet stuAndProSet) throws AIstudyException{
        stuAndProSetService.updateStuAndSet(stuAndProSet);
    }

    //分页获取做题的记录
    @GetMapping("")
    @ApiOperation("根据条件获取用户做题的记录")
//    @PreAuthorize("hasAnyAuthority(#stuID.toString())")
    public IPage<StuAndProSet> findStuAndSet(@ApiParam("用户ID")@RequestParam(value = "stuID") Integer stuID,
                                             @ApiParam("套题ID")@RequestParam(value = "proSetID",required = false) Integer proSetID,
                                             @ApiParam("最早时间")@RequestParam(value = "startTime",required = false)Timestamp startTime,
                                             @ApiParam("最晚时间")@RequestParam(value = "endTime",required = false) Timestamp endTime,
                                             @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                             @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){

        Map<String,Object> condition=new HashMap<>();
        condition.put("stuID", stuID);
        if(proSetID!=null) {
            condition.put("proSetID",proSetID);
        }
        if(startTime!=null) {
            condition.put("startTime",startTime);
        }
        if(endTime!=null) {
            condition.put("endTime",endTime);
        }
        return stuAndProSetService.findStuAndSet(condition, pageNum, pageSize);
    }
}
